
package com.yuanma.android.boan;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

/**
 * The launchpad activity for this sample project. This activity launches other activities that
 * demonstrate implementations of common animations.
 */
public class MainActivity extends ListActivity {
    /**
     * This class describes an individual sample (the sample title, and the activity class that
     * demonstrates this sample).
     */
    private class Sample {
        private CharSequence title;
        private Class<? extends Activity> activityClass;

        public Sample(int titleResId, Class<? extends Activity> activityClass) {
            this.activityClass = activityClass;
            this.title = getResources().getString(titleResId);
        }

        @Override
        public String toString() {
            return title.toString();
        }
	public void setTitle(String mtitle)
	{
	    title=mtitle;
	}
	
    }

    /**
     * The collection of all samples in the app. This gets instantiated in {@link
     * #onCreate(android.os.Bundle)} because the {@link Sample} constructor needs access to {@link
     * android.content.res.Resources}.
     */
    private static Sample[] mSamples;
    private Intent mServiceIntent;
    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
	AlarmReceiver alarm = new AlarmReceiver();
	alarm.setAlarm(this);
	
        // Instantiate the list of samples.
        mSamples = new Sample[]{
	    new Sample(R.string.title_login, LoginActivity.class),
	    new Sample(R.string.title_message_read, MessageReadActivity.class),
	    new Sample(R.string.title_message_review, MessageReviewActivity.class),
	    new Sample(R.string.title_qingjia, QingjiaActivity.class),
        };


        setListAdapter(new ArrayAdapter<Sample>(this,
						android.R.layout.simple_list_item_1,
						android.R.id.text1,
						mSamples));
	updateMessageMenu();
        // The filter's action is BROADCAST_ACTION
        IntentFilter statusIntentFilter = new IntentFilter(
							   Constants.BROADCAST_ACTION);
        
        // Sets the filter's category to DEFAULT
        statusIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
        
        // Instantiates a new DownloadStateReceiver
        mDownloadStateReceiver = new DownloadStateReceiver();
        
        // Registers the DownloadStateReceiver and its intent filters
        LocalBroadcastManager.getInstance(this).registerReceiver(
								 mDownloadStateReceiver,
								 statusIntentFilter);
  

    }

    @Override
    protected void onStart()
    {
	super.onStart();
	updateMessageMenu();
	
    }
    

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
        // Launch the sample associated with this list position.
       
	startActivity(new Intent(MainActivity.this, mSamples[position].activityClass));
    }

    private static final String CLASS_TAG = "BoanMainActivity";

    private DownloadStateReceiver mDownloadStateReceiver;

    private class DownloadStateReceiver extends BroadcastReceiver {
        
        private DownloadStateReceiver() {
            
            // prevents instantiation by other packages.
        }
        @Override
        public void onReceive(Context context, Intent intent) {
            /*
             * Gets the status from the Intent's extended data, and chooses the appropriate action
             */
            switch (intent.getIntExtra(Constants.EXTENDED_DATA_STATUS,
                    Constants.STATE_ACTION_COMPLETE)) {
                
                // Logs "started" state
                case Constants.STATE_ACTION_STARTED:
                    if (Constants.LOGD) {
                        
                        Log.d(CLASS_TAG, "State: STARTED");
                    }
                    break;
                // Logs "connecting to network" state
                case Constants.STATE_ACTION_CONNECTING:
                    if (Constants.LOGD) {
                        
                        Log.d(CLASS_TAG, "State: CONNECTING");
                    }
                    break;
                 // Logs "parsing the RSS feed" state
                 case Constants.STATE_ACTION_PARSING:
                    if (Constants.LOGD) {
                        
                        Log.d(CLASS_TAG, "State: PARSING");
                    }
                    break;
                // Logs "Writing the parsed data to the content provider" state
                case Constants.STATE_ACTION_WRITING:
                    if (Constants.LOGD) {
                        
                        Log.d(CLASS_TAG, "State: WRITING");
                    }
                    break;
                // Starts displaying data when the RSS download is complete
                case Constants.STATE_ACTION_COMPLETE:
                    // Logs the status
                    if (Constants.LOGD) {
                        
                        Log.d(CLASS_TAG, "State: COMPLETE");
                    }

		    updateMessageMenu();
		    
                    break;

                case Constants.STATE_ACTION_READ:
		    // after re-login and fetch user's data,go to read
		    startActivity(new Intent(MainActivity.this, MainActivity.class));		    
                    break;
                default:
                    break;
            }
        }
    }
    private void updateMessageMenu()
    {
	String s0 = getResources().getString(R.string.title_login);
	String s1 = getResources().getString(R.string.title_message_read);
	String s2 = getResources().getString(R.string.title_message_review);

	mSamples[0].setTitle(s0.concat(String.format(" - [%s]",JsonPullService.userName.trim())));
	mSamples[1].setTitle(s1.concat(String.format("(%d)",JsonPullService.msgsToRead.size())));
	mSamples[2].setTitle(s2.concat(String.format("(%d)",JsonPullService.msgsToReview.size())));
	
	getListView().invalidateViews();
	
    }
    
}
